package net.ojbk.study.curriculumDesign.service.impl.sall;

import net.ojbk.study.curriculumDesign.beans.canvasmodel.Canvas;
import net.ojbk.study.curriculumDesign.beans.sall.Sall;
import net.ojbk.study.curriculumDesign.dao.sall.SallDao;
import net.ojbk.study.curriculumDesign.dao.canvasmodel.CanvasDao;
import net.ojbk.study.curriculumDesign.service.sall.ISallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("sallService")
public class SallServiceImpl implements ISallService {
    @Autowired
    @Qualifier("sallDao")
    private SallDao sallDao;


    @Autowired
    @Qualifier("canvasDao")
    private CanvasDao canvasDao;
    /**
     * 添加一条拍卖记录
     * @param sall
     * */
    @Override
    public void addOneSall(Sall sall){
        sallDao.insertOne(sall);
    }

    /**
     * 删除一条拍卖记录
     * @param sall
     * */
    @Override
    public void deleteOneSall(Sall sall){
        sallDao.deleteOne(sall);
    }

    /**
    *修改一条拍卖记录
     *有人竞拍，价格更改
     * @param sall
     */
    @Override
    public void updateOneSall(Sall sall){
        sallDao.updateOne(sall);
    }

    /**
     *查找所有记录
     * @return
     * */
    @Override
    public List<Sall> getAllSall(){
        return sallDao.selectAll();
    }

    /**
    * 成功拍卖出去
    * */
    @Override
    public void successSall(Sall sall) {
        Canvas canvas;
        //得到原画对象canvas
        canvas=canvasDao.selectOneCanvas(sall.getCid());
        //更新原画的价值
        canvas.setPrice(sall.getMaxprice());
        //修改原画对象的拥有者为竞拍者user，先删除原画，修改uid,再插入新信息（更改用户后的信息）
        canvasDao.deleteBycid(sall.getCid());
        canvas.setUid(sall.getUid());
        //更新canvas的状态0表示不参与竞拍，1表示不参与竞拍
        sall.setStatus(0);
        sallDao.updateOne(sall);
        //插入信息
        canvasDao.insertOne(canvas);
    }

    /**
     * @return查找指定cid在数据库中的行数
     * */
    @Override
    public int getCount(Sall sall){
        return sallDao.selectCount(sall.getCid());
    }

    /**
     * 根据cid到竞拍对象sall和canvas
     * @return set<>*/
    @Override
    public Set<Object> getSallDetail(int cid) {
        Set<Object> set=new HashSet<>();
        set.add(sallDao.selectOneSall(cid));
        set.add(canvasDao.selectOneCanvas(cid));
        return set;
    }

    /**
     * maintenance  data
     * */
    public void maintenancedata(Date date){
        sallDao.maintendata(date);
    }
}
